const loginform = document.querySelector("#loginform");
const logIn = document.querySelector("#logIn");
const remember = document.querySelector("#remember");

loginform.onsubmit = async function(evt){
    loginerror.style.display = 'none';
    evt.preventDefault()
    // console.log(username.value,password.value)
    let res = await fetch(`http://localhost:3000/users?username=${username.value}&password=${password.value}`)
    .then(res => res.json())
    //登录成功跳转到后台首页，并设置token
    if(res.length > 0){
        location.href = "/code/admin/view/home/index.html"
        localStorage.setItem("token", JSON.stringify({
            ...res[0],
            password:"***" //覆盖密码
        }))
    }else{
        loginerror.style.display = 'block';
        loginerror.style.color = "red"
    }
    //是否保存密码
    if(remember.checked){
        localStorage.setItem(username.value, password.value)
    }
}
//输入用户名后自动填入已保存的密码
username.oninput=function(){
        password.value = localStorage.getItem(username.value)
}